package com.flca.frw.util 
{
	import flash.external.ExternalInterface;
	
	public class UrlQuery 
	{
		private static var instance:UrlQuery;
		
		public var queryString:String;
		public var all:String;
		public var parms:Object;
		
		public static function getInstance():UrlQuery
		{
			if (instance == null)
			{
				instance = new UrlQuery();
				instance.readQueryString();
			}
			
			return instance;
		}

		public function UrlQuery()
		{
			// Private constructor: Singleton
		}
		
		public function getParm(aKey:String):String
		{
			return parms[aKey] as String;
		}

		private function readQueryString():void
		{
			var parmsString:Array;
			var length:int;
			var kvPair:String;
			var index:int;
			var key:String;
			var value:String
					
			parms = {};
			try 
			{
				all =  ExternalInterface.call("window.location.href.toString");
				queryString = ExternalInterface.call("window.location.search.substring", 1);
				
				if (queryString != null)
				{ 
					parmsString = queryString.split('&');
					length = parmsString.length;
					
					for (var i:int=0; i < length; i++) 
					{
						kvPair = parmsString[i];
						index = kvPair.indexOf("=");
						if(index > 0)
						{
							key = kvPair.substring(0,index);
							value = kvPair.substring(index+1);
							parms[key] = value;
						}
					}
				}
				else
				{
					queryString = "";
				}
			}
			catch (e:Error) 
			{ 
				trace("Some error occured. ExternalInterface doesn't work in Standalone player."); 
			}
			
			return;
		}
	}
}
